package com.symone.webcms.model.base;

import com.symone.webcms.common.ResultCode;
import com.xiaoleilu.hutool.json.JSONUtil;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ResultMessage extends HashMap implements Serializable {

    private static ResultMessage result;

    public ResultMessage() {
        super();
    }

    public ResultMessage(Map<String,Object> map){
        super();
        this.putAll(map);
    }

    public ResultMessage(String name,Object value){
        super();
        this.putData(name,value);
    }

    public ResultMessage(Throwable e){
        super();
        putData("code", ResultCode.FAILD.getCode());
        putData("msg",e.getMessage());
    }

    public ResultMessage(ResultCode resultCode){
        super();
        putData("code",resultCode.getCode());
        putData("msg",resultCode.getMsg());
    }

    public void putData(String key,Object obj){
        put(key,obj);
    }

    public static ResultMessage success() {
        result = new ResultMessage();
        result.putData("code", ResultCode.SUCCESS.getCode());
        result.putData("msg", ResultCode.SUCCESS.getMsg());
        return result;
    }

    public static ResultMessage success(String msg) {
        result = new ResultMessage();
        result.putData("code", ResultCode.SUCCESS.getCode());
        result.putData("msg", msg);
        return result;
    }

    public static ResultMessage success(Object data) {
        result = new ResultMessage();
        result.putData("code", ResultCode.SUCCESS.getCode());
        result.putData("msg", ResultCode.SUCCESS.getMsg());
        result.putData("data", data);
        return result;
    }

    public static ResultMessage success(String msg ,Object data) {
        result = new ResultMessage();
        result.putData("code", ResultCode.SUCCESS.getCode());
        result.putData("msg", msg);
        result.putData("data", data);
        return result;
    }

    public static ResultMessage success(String msg ,List data,int total) {
        result = new ResultMessage();
        result.putData("code", ResultCode.SUCCESS.getCode());
        result.putData("msg", msg);
        result.putData("data", data);
        result.putData("count", total);
        return result;
    }

    public static ResultMessage fail() {
        result = new ResultMessage();
        result.putData("code", ResultCode.FAILD.getCode());
        result.putData("msg", ResultCode.FAILD.getMsg());
        return result;
    }

    public static ResultMessage fail(Object data) {
        result = new ResultMessage();
        result.putData("code", ResultCode.FAILD.getCode());
        result.putData("msg", ResultCode.FAILD.getMsg());
        result.putData("data", data);
        return result;
    }

    public static ResultMessage fail(Exception ex) {
        result = new ResultMessage();
        result.putData("code", ResultCode.FAILD.getCode());
        result.putData("msg", ex.getMessage());
        return result;
    }

    public static ResultMessage fail(int code , String msg , Object data) {
        result = new ResultMessage();
        result.putData("code",code);
        result.putData("msg", msg);
        result.putData("data", data);
        return result;
    }

    public static ResultMessage fail(ResultCode resultCode) {
        result = new ResultMessage();
        result.putData("code",resultCode.getCode());
        result.putData("msg", resultCode.getMsg());
        return result;
    }

    public static ResultMessage fail(ResultCode resultCode,Exception ex) {
        result = new ResultMessage();
        result.putData("code",resultCode.getCode());
        result.putData("msg", resultCode.getMsg() + " Exception:" + ex.getMessage());
        return result;
    }

    public static ResultMessage fail(ResultCode resultCode , Object data) {
        result = new ResultMessage();
        result.putData("code",resultCode.getCode());
        result.putData("msg", resultCode.getMsg());
        result.putData("data", data);
        return result;
    }

    public String toJson(){
        return JSONUtil.toJsonStr(this);
    }
}